package com.test.fileapplication;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.test.fileapplication.adapter.FileFragmentSortByTimeAdapter;
import com.test.fileapplication.adapter.FullyGridLayoutManager;
import com.test.fileapplication.adapter.ItemDecoration;
import com.test.fileapplication.base.BaseFragment;
import com.test.fileapplication.base.OnItemClickInterface;
import com.test.fileapplication.util.DensityUtil;
import com.test.fileapplication.util.RemoteFile;
import com.test.fileapplication.util.RemoteFileListSortByTime;
import com.test.fileapplication.util.ResponseFileSearchBean;
import com.test.fileapplication.util.SortByTime;
import com.test.fileapplication.util.TimeUtils;
import com.test.fileapplication.util.ToastUtil;
import com.zxn.drag.ItemDragHelper;
import com.zxn.drag.callback.OnItemDragListener;
import com.zxn.drag.widget.DeleteAreaLayout;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class FileFragment extends BaseFragment implements OnItemClickInterface {


    public static final int requestCodeMove = 1;//移动
    public static final int requestCodeDelete = 400;
    private static final String TAG = "FileFragment";
//    @BindView(R.id.llSearch)
    public LinearLayout llSearch;
//    @BindView(R.id.ivFileEdit)
    public ImageView ivFileEdit;
    public RelativeLayout rlBack;
//    @BindView(R.id.recyFiles)
    public RecyclerView recyFiles;
//    @BindView(R.id.tvSearch)
    public TextView tvSearch;
    //        public VicSlideRecyclerView recyFiles;
//    @BindView(R.id.tv_upload_file)
    public TextView tvUploadFile;
//    @BindView(R.id.emptyPage)
    public LinearLayout emptyPage;
//    @BindView(R.id.swipe_file)
    public ExpandSwipeRefreshLayout swipeFile;
//    @BindView(R.id.ll_empty_page_no_upload)
    public LinearLayout llEmptyPageNoUpload;
//    @BindView(R.id.dal_layout)
    DeleteAreaLayout dalLayout;
//    @BindView(R.id.ll_content)
    LinearLayout llContent;
    int transferNum = 0;
    String json;
    boolean isShow = false;
    private boolean isSelectAll = true;//是否全选
    private FileFragmentSortByTimeAdapter adapter;
    private PopupWindow fileEditPopup;
    private List<RemoteFile> remoteFiles = new ArrayList<>();
    private List<RemoteFile> titleList = new ArrayList<>();//标题List
    private List<RemoteFileListSortByTime> fileByTime = new ArrayList<>();
    private List<RemoteFileListSortByTime> fileByTimeList = new ArrayList<>();
    private List<RemoteFileListSortByTime.ByTimeFile> fileByName = new ArrayList<>();
    private List<RemoteFileListSortByTime.ByTimeFile> fileByNameList = new ArrayList<>();
    private boolean isCreateFolder = false;
    private RemoteFile newFolder;
    private int showIndex;
//    private LocalBroadcastManager localBroadcastManager;
    private List<RemoteFile> canOperationBackUpOrRevertList;
    private List<RemoteFile> deleteFiles = new ArrayList<>();

    public String getCurrentDirID() {
        String dirID = "";
        if (titleList.size() > 0) {
            dirID = titleList.get(titleList.size() - 1).getID();
        }
        return dirID;
    }

    public void setNewFolder(RemoteFile rf) {
        this.isCreateFolder = true;
        this.newFolder = rf;

        if (getActivity() != null) {
            //fragment已经被添加过了
            titleList.clear();//因为此时可能fragment已经有多层目录了，但是现在是重新请求根目录的第一级目录
            setRootDirectory();
            goNext(newFolder);

            isCreateFolder = false;
            newFolder = null;
        } else {
            //fragment还没有被添加
        }
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_file;
    }

    @Override
    protected void setUpView() {

        adapter = new FileFragmentSortByTimeAdapter(getContext());
        adapter.setListener(this);
        recyFiles.addItemDecoration(new ItemDecoration((int) DensityUtil.dip2px(5, getContext()), 0x00fafafa, false));
        recyFiles.setLayoutManager(new FullyGridLayoutManager(getbaseActivity(), 3));
        recyFiles.setAdapter(adapter);
//                adapter.setOnDeleteListener(item -> {
//                    List<RemoteFile> list = new ArrayList<>();
//                    list.add(item);
//                    if (!item.isCanSelected(getContext())) {
//                        ToastUtil.showCenterHasImageToast(getContext(),
//                                getContext().getResources().getString(R.string.public_folder_not_click_tip));
//                        recyFiles.closeMenu();
//                        return;
//                    }
//                    if (item.getBackupStatus() != 1 && item.getRecoverStatus() != 1) {
//                        OperationUtils.deleteRemoteFile(baseActivity, list);
//                    } else {
//                        ToastUtil.showCenterHasImageToast(baseActivity,
//                                getResources().getString(R.string.file_back_up_revert_not_operation));
//                    }
//                });
        recyFiles.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int topRowVerticalPosition =
                        (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 :
                                recyclerView.getChildAt(0).getTop();
                swipeFile.setEnabled(topRowVerticalPosition >= 0 && recyclerView != null && !recyclerView.canScrollVertically(-1));
            }
        });

        swipeFile.setOnRefreshListener(() -> {
            swipeFile.setRefreshing(true);
            onReFresh();
        });


//        initfileRecoveryReceiver();


        swipeFile.setEnabled(false);
        swipeFile.setLongClickable(false);

        onInitItemDragHelper();
    }

    private void initfileRecoveryReceiver() {
//        localBroadcastManager = LocalBroadcastManager.getInstance(getContext());
//        IntentFilter filter = new IntentFilter();
//        filter.addAction(Constants.RECYCLE_RECOVERY_COMPLETE);
//        localBroadcastManager.registerReceiver(fileRecoveryReceiver, filter);
    }

    @Override
    public void onResume() {
        super.onResume();
        //        //刷新数据
        //        swipeFile.setRefreshing(true);
        //        onReFresh();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
//            ((MainActivity) getbaseActivity()).rgParent.setBackgroundColor(Color.parseColor("#fffafafa"));
//            ((MainActivity) getbaseActivity()).getMainBg().setVisibility(View.GONE);
            //((MainActivity) baseActivity).hideVideoView();
        }
    }

    public void refreshCurrentDir(String dirID) {
        if (titleList.size() > 0) {
            RemoteFile file = titleList.get(titleList.size() - 1);
            String currentDir = file.getID();
            if ("我的收藏".equals(file.getFileName()) || dirID.equals(currentDir) || dirID.equals("")) {
                onReFresh();
            }
        } else {
            ToastUtil.showCenterHasImageToast(getbaseActivity(),
                    getResources().getString(R.string.not_get_root_directory));
        }
    }

    @Override
    protected void setUpData() {
        initListener();
        setRootDirectory();

        if (isCreateFolder) {
            goNext(newFolder);
            isCreateFolder = false;
            newFolder = null;
        } else {
//            String shi_min_feed_back_id = PreferencesUtils.getString(getbaseActivity(), Constants.SHI_MIN_FEED_BACK_ID);
//            if (!TextUtils.isEmpty(shi_min_feed_back_id)) {
//                swipeFile.setRefreshing(true);
//                isHideShiMinFeedBack(shi_min_feed_back_id);
//            } else {
            swipeFile.setRefreshing(true);
            getFileList(titleList.get(0), "");
//            }
        }
    }

    private void getFileList(RemoteFile remoteFile, String collectStatus) {
        recyFiles.setVisibility(View.GONE);
        json = "[{\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"冰封文件\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583507758000\",\n" +
                "            \"updateTime\": \"1584028505000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": 13\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"我的相册\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583464224000\",\n" +
                "            \"updateTime\": \"1578879636000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1322942635966496\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"市民反馈\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583464224000\",\n" +
                "            \"updateTime\": \"1584165652000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1329860966350880\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"就够了_2020-02-10 17:45:46\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583464224000\",\n" +
                "            \"updateTime\": \"1583677409000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1332200672854080\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"用户反馈\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583464224000\",\n" +
                "            \"updateTime\": \"1582007598000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1332368476471328\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"各种图片\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583464224000\",\n" +
                "            \"updateTime\": \"1584034680000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1335054137557024\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"新建文件夹\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583515839000\",\n" +
                "            \"updateTime\": \"1583663771000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1335531487100960\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"苏昊\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583568533000\",\n" +
                "            \"updateTime\": \"1583663223000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1335633853284384\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"苏昊(1)\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583599317000\",\n" +
                "            \"updateTime\": \"1583662958000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1335706553155616\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"新建文件夹1\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583648850000\",\n" +
                "            \"updateTime\": \"1583829910000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1335810431385632\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"新建文件夹2\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583666199000\",\n" +
                "            \"updateTime\": \"1583699985000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1335846814875680\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"aaaaaa5558\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583670949000\",\n" +
                "            \"updateTime\": \"1583677701000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1335856776347680\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"新建文件夹3\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583671707000\",\n" +
                "            \"updateTime\": \"1583671707000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1335858365988896\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"测试\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583980720000\",\n" +
                "            \"updateTime\": \"1584188604000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336505949749280\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"测试\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583997430000\",\n" +
                "            \"updateTime\": \"1584178312000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336540988964896\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"测试\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583999155000\",\n" +
                "            \"updateTime\": \"1583999155000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336544606552096\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"新建文件夹4\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1584024143000\",\n" +
                "            \"updateTime\": \"1584026052000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336597477851168\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"测试\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1584063450000\",\n" +
                "            \"updateTime\": \"1584331707000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336679440842784\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"测试\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1584063732000\",\n" +
                "            \"updateTime\": \"1584063732000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336680032239648\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"测试\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1584064651000\",\n" +
                "            \"updateTime\": \"1584064651000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336681957425184\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"测试\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1584072217000\",\n" +
                "            \"updateTime\": \"1584072217000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336698380222496\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"测试\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1584080105000\",\n" +
                "            \"updateTime\": \"1584080105000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336714364715040\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"单元测试1\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1584080513000\",\n" +
                "            \"updateTime\": \"1584080513000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336715220353152\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"测试\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1584092777000\",\n" +
                "            \"updateTime\": \"1584092777000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336740937728032\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"新建文件夹5\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1584164936000\",\n" +
                "            \"updateTime\": \"1584164936000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336892742172704\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"测试删除\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1584271265000\",\n" +
                "            \"updateTime\": \"1584271287000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1337115730247712\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"解封文件\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583464224000\",\n" +
                "            \"updateTime\": \"1583690901000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"2\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"消息文件\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583464224000\",\n" +
                "            \"updateTime\": \"1578879636000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"3\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"我的收藏\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583464224000\",\n" +
                "            \"updateTime\": \"1578879636000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"4\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"duration\": \"\",\n" +
                "            \"fileName\": \"回收站\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"fileSize\": 0,\n" +
                "            \"deleteTime\": \"1583464224000\",\n" +
                "            \"updateTime\": \"1583663771000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"5\",\n" +
                "            \"category\": \"2\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"residueTime\": \"\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \"1210239901\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1583600617000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"duration\": \"0\",\n" +
                "            \"backupStatus\": 3,\n" +
                "            \"fileSize\": \"5764\",\n" +
                "            \"deleteTime\": \"1583673797000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1335709275258912\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"0\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \".adfwcgvbe.dat\",\n" +
                "            \"thumbnail\": \"\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1583770430000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"duration\": \"0\",\n" +
                "            \"backupStatus\": 3,\n" +
                "            \"fileSize\": \"38\",\n" +
                "            \"deleteTime\": \"1584028819000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336065401028640\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"0\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \"Screenrecorder-2020-03-08-18-50-16-33.mp4\",\n" +
                "            \"thumbnail\": \"http://49.7.59.236:13800/calabash/file/thumbnail/1336408373461024\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1583934039000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"duration\": \"28\",\n" +
                "            \"backupStatus\": 0,\n" +
                "            \"fileSize\": \"10645875\",\n" +
                "            \"deleteTime\": \"1583934039000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336408373461024\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"1\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \"1582989737163.mp4\",\n" +
                "            \"thumbnail\": \"http://49.7.59.236:13800/calabash/file/thumbnail/1336408373461088\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1583933974000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"duration\": \"5\",\n" +
                "            \"backupStatus\": 0,\n" +
                "            \"fileSize\": \"1058979\",\n" +
                "            \"deleteTime\": \"1583933974000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336408373461088\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"1\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \"Screenrecorder-2020-03-09-01-02-12-798.mp4\",\n" +
                "            \"thumbnail\": \"http://49.7.59.236:13800/calabash/file/thumbnail/1336408373461120\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1583934770000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"duration\": \"58\",\n" +
                "            \"backupStatus\": 0,\n" +
                "            \"fileSize\": \"15147879\",\n" +
                "            \"deleteTime\": \"1583934770000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336408373461120\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"1\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \"【www.zxit8.com】 5-5 groovy文件处理详解（上）.mp4\",\n" +
                "            \"thumbnail\": \"http://49.7.59.236:13800/calabash/file/thumbnail/1336408373461184\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1583934331000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"duration\": \"981\",\n" +
                "            \"backupStatus\": 0,\n" +
                "            \"fileSize\": \"100057698\",\n" +
                "            \"deleteTime\": \"1583934331000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336408373461184\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"1\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \"【www.zxit8.c 自定义Plugin实战上.mp4\",\n" +
                "            \"thumbnail\": \"http://49.7.59.236:13800/calabash/file/thumbnail/1336408373461216\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1583934115000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"duration\": \"641\",\n" +
                "            \"backupStatus\": 0,\n" +
                "            \"fileSize\": \"87691290\",\n" +
                "            \"deleteTime\": \"1584326871000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336408373461216\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"1\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \"Screenrecorder-2020-03-09-01-00-27-672.mp4\",\n" +
                "            \"thumbnail\": \"http://49.7.59.236:13800/calabash/file/thumbnail/1336408373461248\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1583934003000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"duration\": \"82\",\n" +
                "            \"backupStatus\": 0,\n" +
                "            \"fileSize\": \"12781287\",\n" +
                "            \"deleteTime\": \"1583934003000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336408373461248\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"1\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \"VID_20200301_161527.mp4\",\n" +
                "            \"thumbnail\": \"http://49.7.59.236:13800/calabash/file/thumbnail/1336408373461312\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1583933979000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"duration\": \"5\",\n" +
                "            \"backupStatus\": 0,\n" +
                "            \"fileSize\": \"1703389\",\n" +
                "            \"deleteTime\": \"1583933979000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336408373461312\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"1\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \"Screenrecorder-2020-03-08-14-30-05-202.mp4\",\n" +
                "            \"thumbnail\": \"http://49.7.59.236:13800/calabash/file/thumbnail/1336408373461376\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1583933992000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"duration\": \"8\",\n" +
                "            \"backupStatus\": 0,\n" +
                "            \"fileSize\": \"5171023\",\n" +
                "            \"deleteTime\": \"1583933992000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336408373461376\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"1\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \"Screenrecorder-2020-03-03-17-04-51-861.mp4\",\n" +
                "            \"thumbnail\": \"http://49.7.59.236:13800/calabash/file/thumbnail/1336408373461408\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1583933983000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"duration\": \"11\",\n" +
                "            \"backupStatus\": 0,\n" +
                "            \"fileSize\": \"1091271\",\n" +
                "            \"deleteTime\": \"1583933983000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336408373461408\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"1\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \"1582989607679.mp4\",\n" +
                "            \"thumbnail\": \"http://49.7.59.236:13800/calabash/file/thumbnail/1336408373461440\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1583933978000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"duration\": \"4\",\n" +
                "            \"backupStatus\": 0,\n" +
                "            \"fileSize\": \"761081\",\n" +
                "            \"deleteTime\": \"1583933978000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336408373461440\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"1\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \"ed96aa4a-28cc-46de-8b68-b25e1e4a4d11.jpg\",\n" +
                "            \"thumbnail\": \"http://49.7.59.236:13800/calabash/file/thumbnail/1336567211753504\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1584009712000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"1\",\n" +
                "            \"duration\": \"0\",\n" +
                "            \"backupStatus\": 0,\n" +
                "            \"fileSize\": \"47036\",\n" +
                "            \"deleteTime\": \"1584052811000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336567211753504\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"2\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \"8abe45ab-9a2d-48c3-a223-d1fdb4c300b6.jpg\",\n" +
                "            \"thumbnail\": \"http://49.7.59.236:13800/calabash/file/thumbnail/1336567243210784\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1584009726000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"1\",\n" +
                "            \"duration\": \"0\",\n" +
                "            \"backupStatus\": 0,\n" +
                "            \"fileSize\": \"44700\",\n" +
                "            \"deleteTime\": \"1584052812000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1336567243210784\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"2\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \"ac04bdff-4f3d-4fa3-9eec-361e628f9de9.jpg\",\n" +
                "            \"thumbnail\": \"http://49.7.59.236:13800/calabash/file/thumbnail/1337109239562272\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1584268171000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"duration\": \"0\",\n" +
                "            \"backupStatus\": 0,\n" +
                "            \"fileSize\": \"86641\",\n" +
                "            \"deleteTime\": \"1584268171000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1337109239562272\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"2\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \"yufumao_hulu.gif\",\n" +
                "            \"thumbnail\": \"http://49.7.59.236:13800/calabash/file/thumbnail/1337239772594208\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1584330413000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"duration\": \"0\",\n" +
                "            \"backupStatus\": 0,\n" +
                "            \"fileSize\": \"88587\",\n" +
                "            \"deleteTime\": \"1584330413000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1337239772594208\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"2\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \"Screenshot_2020-03-03-17-20-51-222_com.ehualu.calabashandroid.jpg\",\n" +
                "            \"thumbnail\": \"http://49.7.59.236:13800/calabash/file/thumbnail/1337242415005728\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1584331674000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"duration\": \"0\",\n" +
                "            \"backupStatus\": 0,\n" +
                "            \"fileSize\": \"322158\",\n" +
                "            \"deleteTime\": \"1584331674000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1337242415005728\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"2\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"fileName\": \"SVID_20200223_185917_1.mp4\",\n" +
                "            \"thumbnail\": \"http://49.7.59.236:13800/calabash/file/thumbnail/1337243251769376\",\n" +
                "            \"recoverStatus\": 0,\n" +
                "            \"updateTime\": \"1584332075000\",\n" +
                "            \"parentId\": \"0\",\n" +
                "            \"residueTime\": \"\",\n" +
                "            \"labels\": \"\",\n" +
                "            \"duration\": \"18\",\n" +
                "            \"backupStatus\": 0,\n" +
                "            \"fileSize\": \"8014820\",\n" +
                "            \"deleteTime\": \"1584332075000\",\n" +
                "            \"location\": \"\",\n" +
                "            \"ID\": \"1337243251769376\",\n" +
                "            \"category\": \"1\",\n" +
                "            \"fileType\": \"1\"\n" +
                "        }]";

        Gson gson = new Gson();
        List<ResponseFileSearchBean.DataBean> lists = new ArrayList<ResponseFileSearchBean.DataBean>();
        lists = gson.fromJson(json, new TypeToken<List<ResponseFileSearchBean.DataBean>>() {
        }.getType());

        ResponseFileSearchBean responseFileSearchBean = new ResponseFileSearchBean();
        responseFileSearchBean.setStatus(200);
        responseFileSearchBean.setMessage("成功");
        responseFileSearchBean.setSuccess(true);
        responseFileSearchBean.setTotalcount(48);
        responseFileSearchBean.setData(lists);
        initData(responseFileSearchBean, remoteFile.getID());
    }

    /**
     * 设置根目录
     */
    private void setRootDirectory() {
        RemoteFile remoteFile = new RemoteFile();
        remoteFile.setID("0");
        remoteFile.setFileName("文件");
        titleList.add(remoteFile);
    }

    private void initListener() {
        tvSearch.setOnClickListener(this);
    }

//    private void isHideShiMinFeedBack(String shi_min_feed_back_id) {
//        ApiRetrofit.getInstance().getFolderInfo(shi_min_feed_back_id)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Observer<ResponseFolderInfoBean>() {
//
//                    @Override
//                    public void onSubscribe(Disposable d) {
//
//                    }
//
//                    @Override
//                    public void onNext(ResponseFolderInfoBean responseFolderInfoBean) {
//                        hideLoadingDialog();
//                        LogUtils.d(responseFolderInfoBean.toString());
//                        if (responseFolderInfoBean.isSuccess() && Long.parseLong(String.valueOf(Math.round(responseFolderInfoBean.getData().getDIRSIZE()))) > 0) {
//                            isShow = true;
//                            swipeFile.setRefreshing(true);
//                            getFileList(titleList.get(0), "");
//                        } else {
//                            isShow = false;
//                            swipeFile.setRefreshing(true);
//                            getFileList(titleList.get(0), "");
//                        }
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        hideLoadingDialog();
//                        isShow = false;
//                        swipeFile.setRefreshing(true);
//                        getFileList(titleList.get(0), "");
//                    }
//
//                    @Override
//                    public void onComplete() {
//
//                    }
//                });
//    }

    private synchronized void initData(ResponseFileSearchBean responseFileSearchBean, String parentId) {
        hideLoadingDialog();
        swipeFile.setRefreshing(false);

        if (responseFileSearchBean == null) {
            ToastUtil.showCenterHasImageToast(getbaseActivity(), getString(R.string.wangluocuowu));

        } else {
            if (!responseFileSearchBean.isSuccess()) {
                ToastUtil.showCenterHasImageToast(getbaseActivity(), responseFileSearchBean.getMessage());
            }
        }
//        RefreshFileUtils.newUploadTasks.clear();
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {

                syncSetData(responseFileSearchBean, parentId);

                //去掉回收站
                Iterator<RemoteFile> iterator = remoteFiles.iterator();
                while (iterator.hasNext()) {
                    RemoteFile rf = iterator.next();
                    if ("2".equals(rf.getCategory()) && "0".equals(rf.getParentId()) && rf.getFileName().equals(getbaseActivity().getResources().getString(R.string.recycle_bin))) {
                        iterator.remove();
                    }

                    if ("2".equals(rf.getCategory()) && "0".equals(rf.getParentId()) && rf.getFileName().equals(getbaseActivity().getResources().getString(R.string.yonghufankui))) {
                        iterator.remove();
                    }
//                    LogUtils.d("initData===isShow===" + isShow);
                    if ("2".equals(rf.getCategory()) && "0".equals(rf.getParentId()) && rf.getFileName().equals(getbaseActivity().getResources().getString(R.string.shiminfankui)) && !isShow) {
                        iterator.remove();
                    }
                }

                Collections.sort(remoteFiles, getsortByName());
                convert();
                emitter.onNext("success");
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {
                        if (getsortType() == SORT_BY_NAME) {
                            if (getviewType() == GRID_VIEW) {
                                ((FullyGridLayoutManager) recyFiles.getLayoutManager()).setSpanCount(3);
                                adapter.setDatas(fileByName);
                            } else {
                                ((FullyGridLayoutManager) recyFiles.getLayoutManager()).setSpanCount(1);
                                adapter.setDatas(fileByNameList);
                            }
                        } else {
                            if (getviewType() == GRID_VIEW) {
                                ((FullyGridLayoutManager) recyFiles.getLayoutManager()).setSpanCount(3);
                                adapter.setDatas(fileByTime);
                            } else {
                                ((FullyGridLayoutManager) recyFiles.getLayoutManager()).setSpanCount(1);
                                adapter.setDatas(fileByTimeList);
                            }
                            adapter.expandAll();
                        }

                        if (titleList.size() > 1) {
                            RemoteFile remoteFile = titleList.get(1);
                            if ((remoteFile.getParentId().equals("0") && remoteFile.getFileName().equals(getResources().getString(R.string.received_file))) ||
                                    (remoteFile.getParentId().equals("0") && remoteFile.getFileName().equals(getResources().getString(R.string.shiminfankui))) ||
                                    (remoteFile.getParentId().equals("0") && remoteFile.getFileName().equals(getResources().getString(R.string.yonghufankui)))
                                    || (remoteFile.getParentId().equals("0") && remoteFile.getFileName().equals(getResources().getString(R.string.my_collect)))
                                    || (remoteFile.getParentId().equals("0") && remoteFile.getFileName().equals(getResources().getString(R.string.back_up_revert)))) {
//                                ivUpload.setVisibility(View.GONE);
//                                ivTransfer.setVisibility(View.GONE);
//                                tvTransferNum.setVisibility(View.GONE);
                                if (remoteFiles.size() == 0) {
                                    recyFiles.setVisibility(View.GONE);
                                    emptyPage.setVisibility(View.GONE);
                                    llEmptyPageNoUpload.setVisibility(View.VISIBLE);
                                } else {
                                    recyFiles.setVisibility(View.VISIBLE);
                                    emptyPage.setVisibility(View.GONE);
                                    llEmptyPageNoUpload.setVisibility(View.GONE);
                                }
                            } else {
//                                ivUpload.setVisibility(View.VISIBLE);
//                                ivTransfer.setVisibility(View.VISIBLE);
//                                if (transferNum > 0) {
//                                    tvTransferNum.setVisibility(View.VISIBLE);
//                                } else {
//                                    tvTransferNum.setVisibility(View.GONE);
//                                }
                                if (remoteFiles.size() == 0) {
                                    recyFiles.setVisibility(View.GONE);
                                    emptyPage.setVisibility(View.VISIBLE);
                                    llEmptyPageNoUpload.setVisibility(View.GONE);
                                } else {
                                    recyFiles.setVisibility(View.VISIBLE);
                                    emptyPage.setVisibility(View.GONE);
                                    llEmptyPageNoUpload.setVisibility(View.GONE);
                                }
                            }
                        } else {
//                            ivUpload.setVisibility(View.VISIBLE);
//                            ivTransfer.setVisibility(View.VISIBLE);
//                            if (transferNum > 0) {
//                                tvTransferNum.setVisibility(View.VISIBLE);
//                            } else {
//                                tvTransferNum.setVisibility(View.GONE);
//                            }
                            if (remoteFiles.size() == 0) {
                                recyFiles.setVisibility(View.GONE);
                                emptyPage.setVisibility(View.VISIBLE);
                                llEmptyPageNoUpload.setVisibility(View.GONE);
                            } else {
                                recyFiles.setVisibility(View.VISIBLE);
                                emptyPage.setVisibility(View.GONE);
                                llEmptyPageNoUpload.setVisibility(View.GONE);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private synchronized void syncSetData(ResponseFileSearchBean responseFileSearchBean, String parentId) {

        Gson gson = new Gson();
        List<RemoteFile> lists = new ArrayList<RemoteFile>();
        lists = gson.fromJson(json, new TypeToken<List<RemoteFile>>() {
        }.getType());

        remoteFiles.clear();
        remoteFiles.addAll(lists);
    }

//    @OnClick({R.id.ivFileEdit})
//    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivFileEdit:
                showOperationPopup();
                break;
            case R.id.rl03:

                //切换到缩略图模式，首先判断当前排序规则
                if (getviewType() == GRID_VIEW) {
                    //当前已经是缩略图模式
                    return;
                }
                viewType = GRID_VIEW;
                ((FullyGridLayoutManager) recyFiles.getLayoutManager()).setSpanCount(3);
                if (getsortType() == SORT_BY_NAME) {
                    adapter.setDatas(fileByName);
                } else if (getsortType() == SORT_BY_TIME) {
                    adapter.setDatas(fileByTime);
                    adapter.expandAll();
                }

                if (fileEditPopup != null && fileEditPopup.isShowing()) {
                    fileEditPopup.dismiss();
                }
                isSelectStatus(false);
                break;
            case R.id.rl04:

                //切换到列表模式，首先判断当前排序规则
                if (getviewType() == LIST_VIEW) {
                    //当前已经是列表模式
                    return;
                }
                viewType = LIST_VIEW;
                ((FullyGridLayoutManager) recyFiles.getLayoutManager()).setSpanCount(1);
                if (getsortType() == SORT_BY_NAME) {
                    adapter.setDatas(fileByNameList);
                } else if (getsortType() == SORT_BY_TIME) {
                    adapter.setDatas(fileByTimeList);
                    adapter.expandAll();
                }

                if (fileEditPopup != null && fileEditPopup.isShowing()) {
                    fileEditPopup.dismiss();
                }
                isSelectStatus(false);
                break;
            case R.id.rl05:


                //进入选择模式
                //隐藏底部弹出框
                if (fileEditPopup != null && fileEditPopup.isShowing()) {
                    fileEditPopup.dismiss();
                }
                //更新adapter的选中状态
                isSelectStatus(true);
                break;


            case R.id.rl02:

                if (getsortType() == SORT_BY_NAME) {
                    return;
                }

                sortType = SORT_BY_NAME;

                if (getviewType() == GRID_VIEW) {
                    adapter.setDatas(fileByName);
                } else if (getviewType() == LIST_VIEW) {
                    adapter.setDatas(fileByNameList);
                }

                if (fileEditPopup != null && fileEditPopup.isShowing()) {
                    fileEditPopup.dismiss();
                }
                isSelectStatus(false);
                break;
            case R.id.rl01:

                //按时间排序，首先判断当前视图样式
                if (getsortType() == SORT_BY_TIME) {
                    return;
                }

                sortType = SORT_BY_TIME;
                if (getviewType() == GRID_VIEW) {
                    adapter.setDatas(fileByTime);
                } else if (getviewType() == LIST_VIEW) {
                    adapter.setDatas(fileByTimeList);
                }
                adapter.expandAll();

                if (fileEditPopup != null && fileEditPopup.isShowing()) {
                    fileEditPopup.dismiss();
                }

                isSelectStatus(false);
                break;

        }
    }

    public void convert() {
        fileByName.clear();
        fileByNameList.clear();
        fileByTime.clear();
        fileByTimeList.clear();
        for (RemoteFile rf : remoteFiles) {
            fileByName.add(new RemoteFileListSortByTime.ByTimeFile(rf, 1));
            fileByNameList.add(new RemoteFileListSortByTime.ByTimeFile(rf, 2));
        }

        Map<String, List<RemoteFile>> map = new TreeMap<>(new Comparator<String>() {
            @Override
            public int compare(String obj1, String obj2) {
                // 降序排序
                return obj2.compareTo(obj1);
            }
        });
        for (RemoteFile file : remoteFiles) {
            if (!file.isCanSelected(getbaseActivity())) {
                if (map.containsKey("9999-99-99")) {
                    List<RemoteFile> tempList = map.get("9999-99-99");
                    tempList.add(file);
                    map.put("9999-99-99", tempList);
                } else {
                    List<RemoteFile> tempList = new ArrayList<>();
                    tempList.add(file);
                    map.put("9999-99-99", tempList);
                }
            } else {
                long updateTime = file.getUpdateTime();
                String key = TimeUtils.longToString(updateTime, "yyyy-MM-dd");
                if (map.containsKey(key)) {
                    List<RemoteFile> tempList = map.get(key);
                    tempList.add(file);
                    map.put(key, tempList);
                } else {
                    List<RemoteFile> tempList = new ArrayList<>();
                    tempList.add(file);
                    map.put(key, tempList);
                }
            }
        }

        for (Map.Entry<String, List<RemoteFile>> entry : map.entrySet()) {
            RemoteFileListSortByTime remoteFileListSortByTime = new RemoteFileListSortByTime();
            try {
                remoteFileListSortByTime.setTime(TimeUtils.stringToLong(entry.getKey(), "yyyy-MM-dd"));
            } catch (ParseException e) {
                //                e.printStackTrace();
//                LogUtils.e("error", e);
            }

            Collections.sort(entry.getValue(), new SortByTime());

            remoteFileListSortByTime.setList(entry.getValue());
            remoteFileListSortByTime.setShowDate(entry.getKey());
            fileByTime.add(remoteFileListSortByTime);

            RemoteFileListSortByTime ocFileListByTime2 = new RemoteFileListSortByTime();
            ocFileListByTime2.setList(entry.getValue(), 2);
            ocFileListByTime2.setShowDate(entry.getKey());
            fileByTimeList.add(ocFileListByTime2);
        }
    }

    public void setTextViewSelected(TextView tv, ImageView duigou) {
        Drawable leftDrawable;
        switch (tv.getId()) {
            case R.id.tvTimeSort:
                leftDrawable = getbaseActivity().getResources().getDrawable(R.drawable.bottom_popup_time_sort_blue);
                break;
            case R.id.tvNameSort:
                leftDrawable = getbaseActivity().getResources().getDrawable(R.drawable.bottom_popup_name_sort_blue);
                break;
            case R.id.tvGrid:
                leftDrawable = getbaseActivity().getResources().getDrawable(R.drawable.bottom_popup_grid_blue);
                break;
            case R.id.tvList:
                leftDrawable = getbaseActivity().getResources().getDrawable(R.drawable.bottom_popup_list_blue);
                break;
            default:
                leftDrawable = getbaseActivity().getResources().getDrawable(R.drawable.bottom_popup_select_black);
                break;
        }
        leftDrawable.setBounds(0, 0, DensityUtil.dip2px(22, getbaseActivity()), DensityUtil.dip2px(22,
                getbaseActivity()));
        tv.setCompoundDrawables(leftDrawable, null, null, null);
        tv.setTextColor(Color.parseColor("#5BA0E9"));
        duigou.setVisibility(View.VISIBLE);
    }

    /**
     * 显示操作的底部弹窗
     */
    public void showOperationPopup() {

        Window window = getbaseActivity().getWindow();
        View contentView = View.inflate(getbaseActivity(), R.layout.popup_bottom_edit, null);

        RelativeLayout rl01 = contentView.findViewById(R.id.rl01);
        RelativeLayout rl02 = contentView.findViewById(R.id.rl02);
        RelativeLayout rl03 = contentView.findViewById(R.id.rl03);
        RelativeLayout rl04 = contentView.findViewById(R.id.rl04);
        RelativeLayout rl05 = contentView.findViewById(R.id.rl05);

        rl01.setOnClickListener(this);
        rl02.setOnClickListener(this);
        rl03.setOnClickListener(this);
        rl04.setOnClickListener(this);
        rl05.setOnClickListener(this);

        TextView tvTimeSort = contentView.findViewById(R.id.tvTimeSort);
        TextView tvNameSort = contentView.findViewById(R.id.tvNameSort);
        TextView tvGrid = contentView.findViewById(R.id.tvGrid);
        TextView tvList = contentView.findViewById(R.id.tvList);

        ImageView dg01 = contentView.findViewById(R.id.dg01);
        ImageView dg02 = contentView.findViewById(R.id.dg02);
        ImageView dg03 = contentView.findViewById(R.id.dg03);
        ImageView dg04 = contentView.findViewById(R.id.dg04);

        if (getsortType() == SORT_BY_TIME) {
            setTextViewSelected(tvTimeSort, dg01);
        } else if (getsortType() == SORT_BY_NAME) {
            setTextViewSelected(tvNameSort, dg02);
        }

        if (getviewType() == GRID_VIEW) {
            setTextViewSelected(tvGrid, dg03);
        } else if (getviewType() == LIST_VIEW) {
            setTextViewSelected(tvList, dg04);
        }

        fileEditPopup = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = 0.8f;
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        window.setAttributes(lp);

        fileEditPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp2 = window.getAttributes();
                lp2.alpha = 1f;
                window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                window.setAttributes(lp2);
            }
        });
        fileEditPopup.setOutsideTouchable(true);
        fileEditPopup.setFocusable(true);
        fileEditPopup.showAtLocation(ivFileEdit, Gravity.LEFT | Gravity.BOTTOM, 0, 0);
    }

    /**
     * add by houxiansheng 2019-12-12 11:58:43 是否是选择状态
     */
    public void isSelectStatus(boolean isSelectStatus) {
//        setSelectMode(isSelectStatus);
//        if (isSelectStatus) {
//            llTopTitle.setVisibility(View.GONE);
//            llTopSelect.setVisibility(View.VISIBLE);
//            adapter.setSelectMode(true);
//            ((MainActivity) getbaseActivity()).setMenuShowStatus(false);
//            llBottomEdit.setVisibility(View.VISIBLE);
//            adapter.notifyDataSetChanged();
//            //            Animation shakeAnim = AnimationUtils.loadAnimation(getbaseActivity(), R.anim.shake_y);
//            //            llBottomEdit.startAnimation(shakeAnim);
//        } else {
//            llTopTitle.setVisibility(View.VISIBLE);
//            llTopSelect.setVisibility(View.GONE);
//            adapter.setSelectMode(false);
//            ((MainActivity) getbaseActivity()).setMenuShowStatus(true);
//            adapter.removeAllCheckedList();
//            llBottomEdit.setVisibility(View.GONE);
//            adapter.notifyDataSetChanged();
//            //            Animation shakeAnim = AnimationUtils.loadAnimation(getbaseActivity(), R.anim
//            //            .translate_dialog_out);
//            //            llBottomEdit.startAnimation(shakeAnim);
//        }
    }

    /**
     * add by houxiansheng 2019-12-12 15:36:08 全选、全不选
     */
    public void SelectedAllOrNot() {
        int size = adapter.getCanEditFileTotal();
        if (adapter.getCheckedList().size() < size) {
            adapter.removeAllCheckedList();
            if (getsortType() == SORT_BY_NAME) {
                for (RemoteFileListSortByTime.ByTimeFile byTimeFile : fileByName) {
                    if (byTimeFile.getFile().isCanSelected(getbaseActivity())
                            && byTimeFile.getFile().canSelectedBackUpOrRevert()
                            && byTimeFile.getFile().canOperation()
                            && byTimeFile.getFile().canUpload())
                        adapter.addCheckList(byTimeFile.getFile());
                }
            } else if (getsortType() == SORT_BY_TIME) {
                for (Object object : fileByTime) {
                    if (object instanceof RemoteFileListSortByTime) {
                        RemoteFileListSortByTime byTime = (RemoteFileListSortByTime) object;
                        for (RemoteFile file : byTime.getList()) {
                            if (file.isCanSelected(getbaseActivity())
                                    && file.canSelectedBackUpOrRevert()
                                    && file.canOperation()
                                    && file.canUpload())
                                adapter.addCheckList(file);
                        }
                    }
                }

            }
            adapter.notifyDataSetChanged();
        } else {
            adapter.removeAllCheckedList();
            adapter.notifyDataSetChanged();
        }
    }

    /**
     * 进入下一级目录
     */
    private void goNext(RemoteFile remoteFile) {
        titleList.add(remoteFile);
        String collectStatus = "";
        if (remoteFile.getFileName().equals(getResources().getString(R.string.my_collect)) && remoteFile.getParentId().equals("0")) {
            collectStatus = "1";
            remoteFile.setID("");
        }
        showLoadingDialog(getbaseActivity());
        getFileList(remoteFile, collectStatus);
        llContent.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.slid_left_in));
    }

    /**
     * 是否是根目录
     */
    public boolean isRootDirectory() {
        if (titleList.size() == 1) {
            return true;
        }
        return false;
    }

    /**
     * 刷新
     */
    public void onReFresh() {
        if (getSelectMode()) {//去掉选择模式
            isSelectStatus(false);
        }
        RemoteFile remoteFile = titleList.get(titleList.size() - 1);
        String collectStatus = "";
        if (remoteFile.getFileName().equals(getResources().getString(R.string.my_collect)) && remoteFile.getParentId().equals("0")) {
            collectStatus = "1";
            remoteFile.setID("");
        }
        swipeFile.setRefreshing(true);
//        String shi_min_feed_back_id = PreferencesUtils.getString(getbaseActivity(), Constants.SHI_MIN_FEED_BACK_ID);
//        if (titleList.size() == 1 && !TextUtils.isEmpty(shi_min_feed_back_id)) {
////            isHideShiMinFeedBack(shi_min_feed_back_id);
//        } else {
        getFileList(remoteFile, collectStatus);
//        }
    }

    @Override
    public void onItemClick(RemoteFile remoteFile) {

    }

    @Override
    public void onItemLongCick(RemoteFile file) {
        isSelectStatus(true);
    }

    @Override
    public void selectedAll() {

    }

    @Override
    public void notSelectedAll() {

    }

    @Override
    public void selectCount(List<RemoteFile> selectedList, int count) {

    }

    public boolean getSelectMode() {
        if (adapter != null) {
            return adapter.isSelectMode();
        }
        return false;
    }

    public void setSelectMode(boolean b) {
        //filefragment的属性
        boolean isSelectMode = b;
        if (b) {
            adapter.setSelectMode(true);
            //将搜索框后面的编辑图标置灰
            ivFileEdit.setImageResource(R.mipmap.frg_file_edit_disabled);
            ivFileEdit.setClickable(false);
        } else {
            adapter.setSelectMode(false);
            ivFileEdit.setImageResource(R.mipmap.frg_file_edit);
            ivFileEdit.setClickable(true);
        }
    }

//    @Override
//    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        //此处可以根据两个Code进行判断，本页面和结果页面跳过来的值
//        switch (requestCode) {
//            case SharePopupWindow.REQUEST_SHARE:
//                isSelectStatus(false);
//                break;
//            case requestCodeMove:
//                if (resultCode == MoveActivity.resultCodeMove) {//移动操作后刷新界面
//                    ToastUtil.showCenterHasImageToast(getbaseActivity(),
//                            getResources().getString(R.string.move_success));
//                } else if (resultCode == MoveActivity.resultExistFileCode) {
//                    //校验目标文件夹是否存在该文件
//                    ToastUtil.showCenterHasImageToast(getbaseActivity(),
//                            getResources().getString(R.string.move_exist_file));
//                } else if (resultCode == MoveActivity.resultExistFolderCode) {
//                    //校验目标文件夹是否存在该文件夹
//                    ToastUtil.showCenterHasImageToast(getbaseActivity(),
//                            getResources().getString(R.string.move_exist_folder));
//                }
//                onReFresh();
//                break;
//            case UploadFileTypeActivity.START_UPLOAD_FILE_TYPE_ACTIVITY:
//                switch (resultCode) {
//                    case RESULT_OK:
//                        //弹出新建文件夹
//                        //检索当前目录的文件夹名称
//                        String text = getResources().getString(R.string.create_folder);
//                        List<String> folders = new ArrayList<>();
//                        for (RemoteFile rf : remoteFiles) {
//                            if ("2".equals(rf.getCategory())) {
//                                folders.add(rf.getFileName());
//                            }
//                        }
//
//                        boolean enter = true;
//                        int i = 1;
//                        while (enter) {
//                            if (folders.contains(text)) {
//                                text = getResources().getString(R.string.create_folder);
//                                text += i;
//                                i++;
//                            } else {
//                                break;
//                            }
//                        }
//                        new CreateFolderAndReNameDialog(getbaseActivity(),
//                               0, text,  this).show();
//                        break;
//                }
//                break;
//            case requestCodeDelete:
//                if (resultCode == EditFileActivity.resultCodeDelete
//                        || resultCode == MoveActivity.resultCodeMove
//                        || resultCode == EditFileActivity.resultCodeRename) {//刷新界面
//                    onReFresh();
//                }
//        }
//    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    private void onInitItemDragHelper() {
        ItemDragHelper mItemDragHelper = new ItemDragHelper(dalLayout);
        mItemDragHelper.setOnDragListener(new OnItemDragListener() {
            @Override
            public void onDragStart() {
                Log.i(TAG, "onDragStart: ");
                swipeFile.setRefreshEnabled(false);
                dalLayout.showDragStartView();
            }

            @Override
            public void onDragFinish(boolean isInside, int position) {
                Log.i(TAG, "onDragFinish: isInside:" + isInside);
                if (isInside) {
                    dalLayout.showDragFinishView();
                    //fileByName.remove(position);
                    //fileByNameList.remove(position);
                    //adapter.getData().remove(position);
                    adapter.notifyDataSetChanged();
                    //todo:删除逻辑自行处理
                } else {
                    dalLayout.hideView();
                }
                swipeFile.setRefreshEnabled(true);
            }

            @Override
            public void onDragAreaChange(boolean isInside, boolean isIdle) {
                Log.i(TAG, "onDragAreaChange: --isInside:" + isInside + "--isIdle:" + isIdle);
                if (isIdle) {
                    return;
                }
                dalLayout.showDragAreaChangeView(isInside);
            }
        });
        mItemDragHelper.attachToRecyclerView(recyFiles);
    }

}
